package com.lzw.bluetoothpractice.Fragment;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.airsaid.pickerviewlibrary.adapter.ArrayWheelAdapter;
import com.airsaid.pickerviewlibrary.listener.OnItemSelectedListener;
import com.alibaba.fastjson.JSONObject;
import com.contrarywind.view.WheelView;
import com.lzw.bluetoothpractice.Activitys.MainActivity;
import com.lzw.bluetoothpractice.Adapters.RecoveryActionListAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models.Kneel;
import com.lzw.bluetoothpractice.Models.Temperature;
import com.lzw.bluetoothpractice.MyApplication;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.Utils.TimeUtils;
import com.lzw.bluetoothpractice.Views.TempControlView;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.LongPredicate;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class KneeFragment extends Fragment {
    private Map<String, String> keyMap;
    Context mcontext;
    View v;
    Button button;
    String deviceId;
    TempControlView tempControl;
    private ArrayList<String> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    OptionsPickerView<String> mOptionsPickerView;
    private Handler mHander=new Handler();
    private  int option=2;
    public KneeFragment(Context context) {
        mcontext=context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v=inflater.inflate(R.layout.knee_check,container,false);
        //rvTodayActionList=v.findViewById(R.id.rcActionList);
        InitView();
        deviceId = SharePreferenceUtils.getString(getContext(),"DeviceId",null);
        mHander.post(timeRunable);
        return v;
    }
    private  void InitView(){

        button=v.findViewById(R.id.floatingActionButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initInsustry();

            }
        });

        tempControl = v.findViewById(R.id.temp_control);
        // 设置三格代表温度1度
        tempControl.setAngleRate(1);
        tempControl.setTemp(0f, 135.0f, Float.parseFloat(MyApplication.getInstance().temperature));
        //设置旋钮是否可旋转
        tempControl.setCanRotate(true);

        tempControl.setOnTempChangeListener(new TempControlView.OnTempChangeListener() {
            @Override
            public void change(float temp) {
                Toast.makeText(getContext(), temp + "°", Toast.LENGTH_SHORT).show();
            }
        });

        tempControl.setOnClickListener(new TempControlView.OnClickListener() {
            @Override
            public void onClick(Float temp) {
                Toast.makeText(getContext(), temp + "°", Toast.LENGTH_SHORT).show();
            }
        });


    }

    public void initInsustry(){

        options1Items.add("〇级");
        options1Items.add( "Ⅰ级");
        options1Items.add("Ⅱ级");
        options1Items.add("Ⅲ级");
        options1Items.add("Ⅳ级");





        //条件选择器
        mOptionsPickerView = new OptionsPickerView<String>(getContext());


        // 设置数据
        mOptionsPickerView.setPicker(options1Items);

        // 设置选项单位
        //mOptionsPickerView.setLabels("性");
        mOptionsPickerView.setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int option1, int option2, int option3) {

                try {
                    keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/message/messageId");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String userid = SharePreferenceUtils.getString(getContext(),"userId",null);
                Kneel kneel=new Kneel();
                kneel.setKneel(MyApplication.getInstance().angle);
                kneel.setUserId(Long.parseLong(userid));
                kneel.setPain(option1);
                kneel.setDeviceId(Long.parseLong(deviceId));
                String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;
                RetrofitManager.getInstance(AppConfig.BASE_URL)
                        .createReq(RequestApi.class).addKneel(kneel,"*:*:*","1", AppUtil.getVersionCode() + "", accessToken, keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                        observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        Log.e("增加屈膝度记录",e.getMessage()+"");

                    }

                    @Override
                    public void onNext(JSONObject json) {



                        if(json.getInteger("code")==200){

                            Toast toast=Toast.makeText(getContext(),"记录数据成功",Toast.LENGTH_LONG);
                            toast.show();


                        }else {
                            Toast toast=Toast.makeText(getContext(),"记录数据失败",Toast.LENGTH_LONG);
                            toast.show();
                        }


                    }
                });
                Temperature temperature=new Temperature();
                temperature.setUserId(Long.parseLong(userid));
                temperature.setDeviceId(Long.parseLong(deviceId));
                temperature.setTemperature(Double.parseDouble(MyApplication.getInstance().temperature));
                RetrofitManager.getInstance(AppConfig.BASE_URL)
                        .createReq(RequestApi.class).addTemp(temperature,"*:*:*","1", AppUtil.getVersionCode() + "", accessToken, keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                        observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        Log.e("增加屈膝度记录",e.getMessage()+"");

                    }

                    @Override
                    public void onNext(JSONObject json) {



                        if(json.getInteger("code")==200){

                            Toast toast=Toast.makeText(getContext(),"记录数据成功",Toast.LENGTH_LONG);
                            toast.show();


                        }else {
                            Toast toast=Toast.makeText(getContext(),"记录数据失败",Toast.LENGTH_LONG);
                            toast.show();
                        }


                    }
                });



            }
        });


        mOptionsPickerView.show();

    }

    private Runnable timeRunable = new Runnable() {
        @Override
        public void run() {


            tempControl.setTemp(Float.parseFloat(MyApplication.getInstance().angle));


            //递归调用本runable对象，实现每隔一秒一次执行任务
            mHander.postDelayed(this, 1000);

        }

    };
}
